package com.ruoyi.project.mobile.home;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.R;
import com.ruoyi.common.utils.RFactory;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.project.system.banner.service.IBannerService;
import com.ruoyi.project.system.customer.customer.domain.Customer;
import com.ruoyi.project.system.customer.customer.service.ICustomerService;
import com.ruoyi.project.system.goods.goods.domain.GoodsGoods;
import com.ruoyi.project.system.goods.goods.enums.GoodsStatus;
import com.ruoyi.project.system.goods.goods.service.IGoodsGoodsService;
import com.ruoyi.project.system.goodsclass.domain.GoodsClass;
import com.ruoyi.project.system.goodsclass.enums.ClassIsHome;
import com.ruoyi.project.system.goodsclass.enums.ClassStatus;
import com.ruoyi.project.system.goodsclass.service.IGoodsClassService;
import com.ruoyi.project.system.hotword.service.IHotWordService;
import com.ruoyi.project.system.house.house.domain.House;
import com.ruoyi.project.system.house.house.enums.HouseDto;
import com.ruoyi.project.system.house.house.service.IHouseService;
import com.ruoyi.project.system.house.img.domain.HouseImg;
import com.ruoyi.project.system.house.img.service.IHouseImgService;
import com.ruoyi.project.system.house.member.domain.MemberHouse;
import com.ruoyi.project.system.house.member.service.IMemberHouseService;
import com.ruoyi.project.system.member.domain.Member;
import com.ruoyi.project.system.member.enums.MemberIsVip;
import com.ruoyi.project.system.member.service.IMemberService;
import com.ruoyi.project.system.proclamation.domain.Proclamation;
import com.ruoyi.project.system.proclamation.enums.ProclamationStatus;
import com.ruoyi.project.system.proclamation.service.IProclamationService;
import com.ruoyi.project.system.shopconfig.service.IShopConfigService;

/**
 * 功能：
 *
 * @author：dell
 * @create：2020/6/18 18:19
 * @version：2020 Version：1.0
 * @company：老屋新房 Created with IntelliJ IDEA
 */
@RestController
@RequestMapping("/api/home")
public class HomeController extends BaseController {

	@Autowired
	private IBannerService bannerService;
	@Autowired
	private IGoodsClassService goodsClassService;
	@Autowired
	private IProclamationService proclamationService;
	@Autowired
	private IGoodsGoodsService goodsGoodsService;
	@Autowired
	private IHotWordService hotWordService;
	@Autowired
	private IShopConfigService shopConfigService;

	@Autowired
	private IHouseService houseService;
	@Autowired
	private IHouseImgService houseImgService;

	@Autowired
	private IMemberHouseService memberHouseService;

	@Autowired
	private ICustomerService customerService;

	@Autowired
	private IMemberService memberService;

	/**
	 * 查询房源列表
	 * @param name
	 * @param sole
	 * @param type
	 * @param recommend
	 * @return
	 */
	@GetMapping("/house")
	public R houseList(@RequestParam(required = false) String uid,
					   @RequestParam(required = false) String cid,
					   @RequestParam(required = false) String name,
					   @RequestParam(required = false) Integer sole,
					   @RequestParam(required = false) Integer type,
					   @RequestParam(required = false) Integer recommend,
					   @RequestParam(required = false,defaultValue = "1") Integer status){
		startPage();
		House house = new House();
		house.setName(name);
		house.setSole(sole);
		house.setType(type);
		house.setIsRecommend(recommend);
		house.setStatus(status);
		house.setMemberId(uid);
		List<HouseDto> houseDtos = this.houseService.selectHouseList(house);
		for(HouseDto houseDto:houseDtos){
			//设置图片
			HouseImg houseImg = new HouseImg();
			houseImg.setHouseId(houseDto.getId());
			List<HouseImg> houseImgs = this.houseImgService.selectHouseImgList(houseImg);
			houseDto.setHouseImgs(houseImgs.isEmpty()?null:Arrays.asList(houseImgs.get(0)));
			//判断是否关注
			MemberHouse memberHouse = new MemberHouse();
			memberHouse.setMemberId(cid);
			memberHouse.setHouseId(houseDto.getId());
			houseDto.setCollection(this.memberHouseService.selectMemberHouseList(memberHouse).size());
		}
		return RFactory.generateR(getDataTable(houseDtos));
	}


	/**
	 * 查询房源详情
	 * @param uid
	 * @param id
	 * @return
	 */
	@GetMapping("/house/info")
	public R findHouseInfo(@RequestParam String uid,@RequestParam String id){
		HouseDto houseDto = this.houseService.selectHouseById(id);
		HouseImg houseImg = new HouseImg();
		houseImg.setHouseId(houseDto.getId());
		List<HouseImg> houseImgs = this.houseImgService.selectHouseImgList(houseImg);
		houseDto.setHouseImgs(houseImgs);
		//判断是否关注
		MemberHouse memberHouse = new MemberHouse();
		memberHouse.setMemberId(uid);
		memberHouse.setHouseId(houseDto.getId());
		houseDto.setCollection(this.memberHouseService.selectMemberHouseList(memberHouse).size());
		return RFactory.generateR(houseDto);
	}

	/**
	 * 关注 取消关注
	 * @param memberHouse
	 * @return
	 */
	@PostMapping("/collection")
	public R memberCollection(@RequestBody MemberHouse memberHouse){
		List<MemberHouse> memberHouses = this.memberHouseService.selectMemberHouseList(memberHouse);
		if(memberHouses.size()==0){
			memberHouse.setCreated(DateUtils.getNowDate());
			this.memberHouseService.insertMemberHouse(memberHouse);
		}else{
			this.memberHouseService.deleteMemberHouseById(memberHouses.get(0).getId());
		}
		return R.ok();
	}

	/**
	 * 查询公告
	 * @param uid
	 * @return
	 */
	@GetMapping("/proclamations")
	private R homeProclamations(@RequestParam String uid){
		Proclamation proclamation = new Proclamation();
		proclamation.setStatus(ProclamationStatus.SHOW.getValue());
		return RFactory.generateR(this.proclamationService.selectProclamationList(proclamation));
	}


	/**
	 *
	 * @param brand 是否是品牌大牌 0 不是 1 是
	 * @param abroad 是否是境外产品 0 不是 1 是
	 * @param recommend 是否是推荐产品 0 不是 1 是
	 * @param hot 是否是热卖产品 0 不是 1 是
	 * @param spike 是否是秒杀产品 0 不是 1 是
	 * @return
	 */
	@GetMapping("/goods")
	private R homeGoods(@RequestParam String uid,
						@RequestParam(required = false,defaultValue = "") Integer brand,
						@RequestParam(required = false,defaultValue = "") Integer abroad,
						@RequestParam(required = false,defaultValue = "") Integer recommend,
						@RequestParam(required = false,defaultValue = "") Integer hot,
						@RequestParam(required = false,defaultValue = "") Integer spike,
						@RequestParam(required = false,defaultValue = "") String name){
		startPage();
		GoodsGoods goodsGoods = new GoodsGoods();
		goodsGoods.setIsAbroad(abroad);
		goodsGoods.setIsBrand(brand);
		goodsGoods.setIsHot(hot);
		goodsGoods.setIsRecommend(recommend);
		goodsGoods.setHasSpike(spike);
		goodsGoods.setStatus(GoodsStatus.UP_SHELF.getValue());
		goodsGoods.setName(name);
		return RFactory.generateR(getDataTable(this.goodsGoodsService.findGoodsDtoList(goodsGoods)));
	}

	/**
	 * 查询热词
	 * @param uid
	 * @return
	 */
	@GetMapping("/hotwords")
	public R homeHotWord(@RequestParam String uid){
		return RFactory.generateR(this.hotWordService.findAll());
	}

	/**
	 * 查询首页配置信息
	 * @param uid
	 * @return
	 */
	@GetMapping("/config")
	private R homeConfig(@RequestParam String uid){
		return RFactory.generateR(this.shopConfigService.findAll().get(0));
	}

	/**
	 * 添加客户
	 * @param customer
	 * @return
	 */
	@PostMapping("/customer")
	@Transactional
	public R addCustomer(@RequestBody Customer customer){
		//判断用户是否是 联盟商
		Member member = this.memberService.selectMemberById(customer.getMemberId());
		if(member==null||member.getIsVip().equals(MemberIsVip.NO.getValue())){
			return R.error("您还不是联盟商");
		}

		//查询用户是否存在
		Customer select = new Customer();
		select.setMobile(customer.getMobile());
		List<Customer> customers = this.customerService.selectCustomerList(select);
		if(customers.size()!=0){
			return R.error("客户已存在");
		}

		//判断房源状态 是否正常
		if(customer.getHouseId()!=null){
			HouseDto houseDto = this.houseService.selectHouseById(customer.getHouseId());
			if(houseDto==null||houseDto.getStatus()!=1){
				return R.error("房源已下架,请重新选择");
			}
		}

		customer.setType(0);
		customer.setStatus(0);
		this.customerService.insertCustomer(customer);
		return R.ok();
	}

}
